<?php

namespace app\admin\model;

use app\admin\traits\ScopeUniacid;
use app\common\model\TimeModel;
use app\common\service\WeginService;
use think\facade\Db;

class GoodCategory extends TimeModel
{
  
     // 定义全局的查询范围
    protected $globalScope = ['uniacid'];
    use ScopeUniacid;

    public function getStatusList()
    {
        return ['0'=>'禁用','1'=>'启用',];
    }

    public function getPidMenuList( )
    {
        $list = $this->field('id, pid,title')
          
            ->where([
                ['status', '=', 1],
            ]) 
            ->order(Db::raw('pid asc,sort=0,sort asc'))
            ->select()
            ->toArray();
        $pidMenuList = $this->buildPidMenu(0, $list);
        $pidMenuList = array_merge([[
            'id' => 0,
            'pid' => 0,
            'title' => '顶级菜单',
            'txt_title' => '顶级菜单',
        ]], $pidMenuList);
        return $pidMenuList;
    }

    protected function buildPidMenu($pid, $list, $level = 0)
    {
        $newList = [];
        foreach ($list as $vo) {
            if($vo['pid'] == $pid) {
                $level++;
                foreach ($newList as $v) {
                    if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
                        $level = $v['level'];
                        break;
                    }
                }
                $vo['level'] = $level;
                $vo['txt_title'] = $vo['title'];
                
                if ($level > 1) {
                    $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $markString = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
                    $vo['title'] = $markString .$vo['title'];
                   
                }
                $newList[] = $vo;
                $childList = $this->buildPidMenu($vo['id'], $list, $level);
                !empty($childList) && $newList = array_merge($newList, $childList);
            }

        }
        return $newList;
    }
    public function getCategoryIdsData( ){
        $categoryLists = [];
        $CategoryIdsData='';
        $pidMenuList = $this->getPidMenuList( );
        if($pidMenuList){
            array_map(function ($category) use (&$categoryLists){
                $categoryLists[]=array(
                    'value'=>$category['id'],
                    'title'=>$category['txt_title'].'['.$category['id'].']'
                );
            },$pidMenuList);
            $CategoryIdsData=json_encode($categoryLists,JSON_UNESCAPED_UNICODE);
        }
        return $CategoryIdsData;
    }
    //获取父类所有孩子节点id
    public function getChildrenIds($id){
        $childrenIds=[];
        $list = $this->field('id,pid,title')->select()->toArray();
        if (is_array($id)) {
            foreach ($id as $vo) {
                $pidMenuList = $this->buildPidMenu($vo, $list);
                foreach ($pidMenuList as $vo){
                    $childrenIds[]=$vo['id'];
                }
            }
        }else{
            $pidMenuList = $this->buildPidMenu($id, $list);
            foreach ($pidMenuList as $vo){
                $childrenIds[]=$vo['id'];
            }
        }

        return $childrenIds;
    }
}